package de.gruessing.gwtsports.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

import de.gruessing.gwtsports.client.services.DataRetriever;
import de.gruessing.gwtsports.client.services.DataRetrieverAsync;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityLapDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrainingDataDTO;

public class HelloGwt implements EntryPoint {

	@Override
	public void onModuleLoad() 
	{
		Label label = new Label("Hello GWT !!!");
		Button button = new Button("Load TCX");
		button.addClickHandler(new ClickHandler() 
		{
			@Override
			public void onClick(ClickEvent event) 
			{
				DataRetrieverAsync xDataRetriever = GWT.create(DataRetriever.class);				
				AsyncCallback<TrainingDataDTO> xCallback = new AsyncCallback<TrainingDataDTO>() 
				{
			    public void onFailure(Throwable caught) 
			    {
			      System.out.println("Das war wohl nix!");
			      caught.printStackTrace();
			    }

					@Override
					public void onSuccess(TrainingDataDTO result)
					{
						for( ActivityDTO xActivity : result.getActivities().getActivity() )
						{
							System.out.println( "ID: " + xActivity.getId() );
							for( ActivityLapDTO xLap : xActivity.getLap() )
							{
								System.out.println( "/t Lap: " + xLap.getTotalTimeSeconds() );
								System.out.println( "/t AVG HR: " + xLap.getAverageHeartRateBpm().getValue() );
							}
						}
					}
			  };
			  xDataRetriever.getTrainingData( "C://Users//Jan//Desktop//Garmin//06.10.2011_17_50_16_history.tcx", xCallback);
			}
		});

		RootPanel.get().add(label);
		RootPanel.get().add(button);
	}
}